
#include	"Ship.h"
#include	"defs.h"
#include	"Weapon.h"

Ship::Ship(){	
	shipState = SHIP_STILL;
	shipGraph = SHIP_STILL;
	shipHealth = 100;
	shipArmor = 0;
	speedX_multiplier = 1.0f;
	speedY_multiplier = 0.5f;
}

bool Ship::init(char* shipImage, shipTypes st){

	if(!Object::init(shipImage))
		return false;

	if(!weapon.init(Weapon::PULSE_GUN, this))
		return false;

	shipType = st;

	speedX_multiplier = 1.2f;
	speedY_multiplier = 0.8f;

	switch(shipType)
	{
		case SHIP_1:
			shipType = SHIP_1;
			shipSpeed = SHIP_1_SPEED;

			collisionRect.x = 8;
			collisionRect.y = 11;
			collisionRect.w = 45;
			collisionRect.h = 29;

		break;
	}

	x = WND_WIDTH / 2 - objectSurface->w / 4;
	y = WND_HEIGHT - objectSurface->h - 10;
	
	return true;
}

void Ship::loop(){

	Uint8* kb = SDL_GetKeyState(NULL);

	if(kb[SDLK_SPACE] || kb[SDLK_RSHIFT] || kb[SDLK_LSHIFT])
		weapon.isFiring = true;
	else
		weapon.isFiring = false;

	if(kb[SDLK_UP] || kb[SDLK_w])
		setForward();
	else
		unsetForward();

	if(kb[SDLK_DOWN] || kb[SDLK_s])
		setBackward();
	else
		unsetBackward();

	if(!kb[SDLK_UP] && !kb[SDLK_w] && !kb[SDLK_DOWN] && !kb[SDLK_s])
		shipGraph = SHIP_STILL;

	if(kb[SDLK_LEFT] || kb[SDLK_a])
		setLeft();
	else
		unsetLeft();

	if(kb[SDLK_RIGHT] || kb[SDLK_d])
		setRight();
	else
		unsetRight();
	
	if( shipState & SHIP_FORWARD )
		speedY = shipSpeed * speedY_multiplier;
	else if( shipState & SHIP_BACKWARD )
			speedY = -shipSpeed * speedY_multiplier;
		 else
			speedY = 0;

	if( shipState & SHIP_LEFT )
		speedX = -shipSpeed * speedX_multiplier;
	else if( shipState & SHIP_RIGHT )
			speedX = shipSpeed * speedX_multiplier;
		 else
			speedX = 0;

	Object::loop();

	if(x < 0)
		x = 0;

	if(y < 0)
		y = 0;

	if(x > WND_WIDTH - objectSurface->w / 2)
		x = WND_WIDTH - objectSurface->w / 2;

	if(y > WND_HEIGHT - objectSurface->h / 2)
		y = WND_HEIGHT - objectSurface->h / 2;

	weapon.loopProjectiles();
}

void Ship::render(SDL_Surface* destSurface){
	
	weapon.renderProjectiles(destSurface);

	switch(shipGraph)
	{
		case SHIP_STILL:
			Surface::OnDraw(destSurface, objectSurface, x, y, 0, 0, 60, 50);
		break;

		case SHIP_FORWARD:
			Surface::OnDraw(destSurface, objectSurface, x, y, 60, 0, 60, 50);
		break;

		case SHIP_BACKWARD:
			Surface::OnDraw(destSurface, objectSurface, x, y, 0, 50, 60, 50);
		break;
	}
}

void Ship::cleanup(){
	Object::cleanup();

	weapon.cleanup();
}

void Ship::setForward(){

	if(shipState & SHIP_BACKWARD)
		return;

	shipState = shipState | SHIP_FORWARD;
	shipGraph = SHIP_FORWARD;
}

void Ship::setBackward(){

	if(shipState & SHIP_FORWARD)
		return;

	shipState = shipState | SHIP_BACKWARD;
	shipGraph = SHIP_BACKWARD;
}

void Ship::setLeft(){

	/*if(shipState & SHIP_RIGHT)
		return;*/

	shipState = shipState | SHIP_LEFT;
}

void Ship::setRight(){

	/*if(shipState & SHIP_LEFT)
		return;*/

	shipState = shipState | SHIP_RIGHT;
}

void Ship::unsetForward(){
	if(shipState & SHIP_FORWARD)
		shipState = shipState ^ SHIP_FORWARD;
}

void Ship::unsetBackward(){
	if(shipState & SHIP_BACKWARD)
		shipState = shipState ^ SHIP_BACKWARD;
}

void Ship::unsetLeft(){
	if(shipState & SHIP_LEFT)
		shipState = shipState ^ SHIP_LEFT;
}

void Ship::unsetRight(){
	if(shipState & SHIP_RIGHT)
		shipState = shipState ^ SHIP_RIGHT;
}

Weapon* Ship::getWeapon(){
	return &weapon;
}

SDL_Rect Ship::getColRect(){
	
	SDL_Rect r;

	r.x = x + collisionRect.x;
	r.y = y + collisionRect.y;
	r.w = collisionRect.w;
	r.h = collisionRect.h;

	return r;
}

void Ship::takeShot(Projectile::projectiles pj){

	int damage;

	switch(pj)
	{
		case Projectile::PULSE:
			damage = 1;
		break;
	}

	damage -= shipArmor;

	if(damage < 0)
		return;

	shipHealth -= damage;

	if(shipHealth < 0)
		shipHealth = 0;
}
